package com.cw.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSONObject;
import com.cw.data.ConstantEnum;
import com.cw.data.PageBean;
import com.cw.data.Result;
import com.cw.pojo.SpecVO;
import com.cw.pojo.TbSpecification;
import com.cw.pojo.TbSpecificationOption;
import com.cw.service.SpacificationService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("spaController")
public class SpacificationController {

    @Reference
    private SpacificationService spacificationService;

    @RequestMapping("toSpacification")
    public String toSpacification() {

        return "admin/specification";
    }

    @RequestMapping("getSpacificationInfo")
    @ResponseBody
    public Result getSpacificationInfo(@RequestParam(defaultValue = "1") Integer pageNumber, @RequestParam(defaultValue = "5")Integer pageSize, String sname) {

        try {

            PageBean<TbSpecification> pa =  spacificationService.getSpacificationInfo(pageNumber, pageSize, sname);

            return Result.result(ConstantEnum.SUCCESS).setData(pa);
        }catch (Exception e) {

            e.printStackTrace();

            return Result.result(ConstantEnum.ERROR);
        }
    }

    @RequestMapping("addSpec")
    @ResponseBody
    public Result addSpec(String options, TbSpecification tbSpecification) {

        try {

            List<TbSpecificationOption> optionList = JSONObject.parseArray(options, TbSpecificationOption.class);

            spacificationService.addSpec(optionList, tbSpecification);

            return Result.result(ConstantEnum.SUCCESS);
        }catch (Exception e) {

            e.printStackTrace();

            return Result.result(ConstantEnum.ERROR);
        }
    }

    @RequestMapping("getOneSpecInfo")
    @ResponseBody
    public Result getOneSpecInfo(Long id) {

        try {

            SpecVO specVO = spacificationService.getOneSpecInfo(id);

            return Result.result(ConstantEnum.SUCCESS).setData(specVO);
        }catch (Exception e) {

            e.printStackTrace();

            return Result.result(ConstantEnum.ERROR);
        }
    }

    @RequestMapping("updateSpec")
    @ResponseBody
    public Result updateSpec(String options, TbSpecification tbSpecification) {

        try {

            List<TbSpecificationOption> optionList = JSONObject.parseArray(options, TbSpecificationOption.class);

            spacificationService.updateSpec(optionList, tbSpecification);

            return Result.result(ConstantEnum.SUCCESS);
        }catch (Exception e) {

            e.printStackTrace();

            return Result.result(ConstantEnum.ERROR);
        }
    }

    @RequestMapping("deleteSpec")
    @ResponseBody
    public Result deleteSpec(Long[] id) {

        try {

            spacificationService.deleteSpec(id);

            return Result.result(ConstantEnum.SUCCESS);
        }catch (Exception e) {

            e.printStackTrace();

            return Result.result(ConstantEnum.ERROR);
        }
    }
}
